package com.scut.online_black_market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scut.online_black_market.dto.*;
import com.scut.online_black_market.entity.ProductSellout;
import com.scut.online_black_market.exception.*;
import com.scut.online_black_market.service.ProductOffShelfService;
import com.scut.online_black_market.service.ProductTradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Optional;

/**
 *  create by yellowBroke on 2020年10月12日 17点09分
 *  商品交易接口
 */

@Api(value = "商品交易接口",tags = "商品交易接口")
@RestController
@RequestMapping(value = "/api/productTrade")
public class ProductTradeController {

    //按照名称注入
    @Resource
    private ProductTradeService productTradeService;

    @Resource
    ProductOffShelfService productOffShelfService;

    @ApiOperation("上架商品")
    @RequestMapping(value = "/sellProduct", method = RequestMethod.POST)
    public ResponseEntity<SuccessResponse> sellProduct(@RequestBody ProductInfo productInfo) {

        try{
            productTradeService.sellProduct(productInfo);
            return ResponseEntity.ok(new SuccessResponse(true, "商品上架成功"));
        } catch (SellProductException e) {
            return ResponseEntity.ok(new SuccessResponse(false, e.getMessage()));
        }
    }

    @ApiOperation("分页查询商品信息")
    @RequestMapping(value = "/queryByPage/{pageNo}/{pageSize}/{queryType}", method = RequestMethod.GET)
    public ResponseEntity<IPage<ProductSellout>> queryCollection(@ApiParam(value = "页号") @PathVariable int pageNo,
                                                                 @ApiParam(value = "页大小") @PathVariable int pageSize,
                                                                 @ApiParam(value = "查询类型") @PathVariable int queryType) {
        // 分页查询
        return ResponseEntity.ok(productTradeService.queryProductByPage(pageNo,pageSize,queryType));
    }

    @ApiOperation("根据商品id查询商品信息")
    @RequestMapping(value = "/queryById/{productId}",method = RequestMethod.GET)
    public ResponseEntity<ProductResponse> queryById(@PathVariable long productId){

        try {
            ProductResponse productResponse = productTradeService.queryById(productId);
            return ResponseEntity.ok(productResponse);
        } catch (UniversalException e) {
            return ResponseEntity.of(Optional.empty());

        }
    }

    @ApiOperation("点击购买按钮")
    @RequestMapping(value="/purchase/{productId}/{buyerId}/{sellmanID}",method=RequestMethod.GET)
    public ResponseEntity<SuccessResponse> purchase(@ApiParam(value="商品id") @PathVariable long productId,
                                                    @ApiParam(value="买家id") @PathVariable String buyerId,
                                                    @ApiParam(value="卖家id") @PathVariable String sellmanID)
    {
        try{
            SuccessResponse successResponse= productTradeService.purchase(productId,buyerId,sellmanID);
            return ResponseEntity.ok(successResponse);
        }catch (UniversalException e)
        {
            SuccessResponse successResponse = SuccessResponse.builder()
                    .status((short)200)
                    .success(false)
                    .message(e.getMessage())
                    .build();
            return ResponseEntity.ok(successResponse);
        }
        catch (BalanceException e)
        {
            SuccessResponse successResponse = SuccessResponse.builder()
                    .message(e.getMessage())
                    .success(false)
                    .build();
            return ResponseEntity.ok(successResponse);
        }
    }

    @ApiOperation("确认收货，完成订单")
    @RequestMapping(value="/tradeFinished/{productId}",method = RequestMethod.GET)
    public ResponseEntity<SuccessResponse> tradeFinished(@ApiParam(value="商品id")@PathVariable long productId)
    {
        SuccessResponse successResponse = null;
        try{
            successResponse= productTradeService.tradeFinished(productId);
            return ResponseEntity.ok(successResponse);
        } catch (UniversalException e) {
            successResponse=new SuccessResponse(false,e.getMessage());
            return ResponseEntity.ok(successResponse);
        }
    }

    @ApiOperation("下架已上架的商品")
    @RequestMapping(value = "/OffShelfByID/{id}", method = RequestMethod.GET)
    public ResponseEntity<SuccessResponse> OffShelfProduct(@PathVariable int id){
        try {
            productOffShelfService.OffShelfProduct(id);
            return ResponseEntity.ok(new SuccessResponse(true,"下架成功"));
        } catch (UniversalException e) {
            return ResponseEntity.ok(new SuccessResponse(true, e.getMessage()));
        }
    }

    /********************************************************************************************************
     * 出售/求购分界线
     */

    @ApiOperation("发布求购商品")
    @RequestMapping(value="/askForProduct",method = RequestMethod.POST)
    public ResponseEntity<SuccessResponse>askForProduct(@RequestBody ShoppingGuideInfo shoppingGuideInfo){
        try{
            productTradeService.askForProduct(shoppingGuideInfo);
            return ResponseEntity.ok(new SuccessResponse(true,"求购发布成功"));
        }catch(AskForProductException e){
            return ResponseEntity.ok(new SuccessResponse(false,e.getMessage()));
        }
    }



    @ApiOperation("下架求购商品")
    @RequestMapping(value = "/OffShelfShoppingGuide/{id}", method = RequestMethod.GET)
    public ResponseEntity<SuccessResponse> OffShelfShoppingGuide(@PathVariable int id){

        try {
            productOffShelfService.OffShelfShoppingGuide(id);
            return ResponseEntity.ok(new SuccessResponse(true,"下架成功"));
        } catch (UniversalException e) {
            return ResponseEntity.ok(new SuccessResponse(true,e.getMessage()));
        }
    }

    /**
        created by xxl at 2020/11/21-9:19
        二手市场被求购的商品的出售按钮点击
        用户选择将自己拥有的商品出售给求购方
     */
    @ApiOperation("点击出售按钮")
    @RequestMapping(value="/sell/{shoppingGuideId}",method=RequestMethod.GET)
    public ResponseEntity<SuccessResponse> sell(@ApiParam(value="商品id") @PathVariable long shoppingGuideId) {
        try{
            SuccessResponse successResponse = productTradeService.sell(shoppingGuideId);
            return ResponseEntity.ok(successResponse);
        }catch (UniversalException e)
        {
            return ResponseEntity.ok(new SuccessResponse(false, e.getMessage()));
        }
    }

    /**
    /* created by xxl at 2020/11/21-10:19
    /* 二手市场被求购的商品的出售按钮点击
    /* 用户选择将自己拥有的商品出售给求购方
    */
    @ApiOperation("求购方确认收货，完成订单")
    @RequestMapping(value="/shoppingGuideFinished/{productId}",method = RequestMethod.GET)
    public ResponseEntity<SuccessResponse> shoppingGuideTradeFinished(@ApiParam(value="商品id")@PathVariable long productId)
    {
        SuccessResponse successResponse;
        try{
            successResponse= productTradeService.shoppingGuideTradeFinished(productId);
            return ResponseEntity.ok(successResponse);
        } catch (UniversalException e) {
            successResponse=new SuccessResponse(false,e.getMessage());
            return ResponseEntity.ok(successResponse);
        }
    }



    @ApiOperation("分页查询求购商品")
    @RequestMapping(value="/queryShoppingGuideByPage/{pageNo}/{pageSize}/{typeId}",method =RequestMethod.GET)
    public ResponseEntity<IPage<ShoppingGuideResponse>> queryShoppingGuideByPage (
            @ApiParam("页号数")@PathVariable short pageNo,
            @ApiParam("页大小")@PathVariable short pageSize,
            @ApiParam("查询种类")@PathVariable short typeId) {

        IPage<ShoppingGuideResponse> result= productTradeService.queryShoppingGuideByPage(pageNo,pageSize,typeId);
        return ResponseEntity.ok(result);
    }
}
